<template>
  <div class="admin-login-container">
    <div class="admin-login-form">
      <h2 class="admin-login-title">后台管理系统</h2>
      <div class="admin-login-logo">
        <img src="/src/assets/logo.png" alt="系统Logo" />
      </div>
      
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          id="username" 
          v-model="form.username" 
          type="text" 
          placeholder="请输入用户名"
          class="form-input"
        />
      </div>
      
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          id="password" 
          v-model="form.password" 
          type="password" 
          placeholder="请输入密码"
          class="form-input"
        />
      </div>
      
      <button 
        class="login-button" 
        :disabled="isLoading"
        @click="handleLogin"
      >
        {{ isLoading ? '登录中...' : '登录' }}
      </button>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminLoginView',
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
      error: '',
      isLoading: false,
    }
  },
  created() {
    // 检查是否已登录
    if (localStorage.getItem('adminLoggedIn') === 'true') {
      this.$router.push('/admin/dashboard')
    }
  },
  methods: {
    async handleLogin() {
      this.error = ''
      
      // 简单验证
      if (!this.form.username || !this.form.password) {
        this.error = '请输入用户名和密码'
        return
      }
      
      this.isLoading = true
      
      try {
        // 模拟登录验证 - 实际项目中应该调用API
        // 检查是否为管理员账号（根据数据库中的预设）
        if ((this.form.username === 'admin' && this.form.password === 'admin123') || 
          (this.form.username === 'editor' && this.form.password === 'editor123')) {
          // 保存登录状态
          localStorage.setItem('adminLoggedIn', 'true')
          localStorage.setItem('adminUser', JSON.stringify({
            username: this.form.username,
            role: this.form.username === 'admin' ? 'admin' : 'editor',
          }))
          
          // 清除普通用户登录状态，避免状态冲突
          localStorage.removeItem('currentUser')
          localStorage.removeItem('rememberMe')
          
          // 登录成功后跳转到后台首页
          this.$router.push('/admin/dashboard')
        } else {
          this.error = '用户名或密码错误，仅管理员和编辑可登录'
        }
      } catch (err) {
        console.error('登录失败:', err)
        this.error = '登录失败，请稍后重试'
      } finally {
        this.isLoading = false
      }
    },
  },
}
</script>

<style scoped>
.admin-login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.admin-login-form {
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  width: 100%;
  max-width: 400px;
}

.admin-login-title {
  text-align: center;
  color: #333;
  margin-bottom: 10px;
  font-size: 24px;
  font-weight: 600;
}

.admin-login-logo {
  text-align: center;
  margin-bottom: 30px;
}

.admin-login-logo img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #555;
  font-weight: 500;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.login-button {
  width: 100%;
  padding: 14px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-top: 10px;
}

.login-button:hover:not(:disabled) {
  background: #5a67d8;
}

.login-button:disabled {
  background: #a0aec0;
  cursor: not-allowed;
}

.error-message {
  margin-top: 20px;
  padding: 12px;
  background: #fed7d7;
  color: #c53030;
  border-radius: 6px;
  text-align: center;
}

@media (max-width: 480px) {
  .admin-login-form {
    padding: 30px 20px;
  }
}
</style>